package com.education.welco.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.education.common.core.BaseController;
import com.education.common.pojo.LCatalog;
import com.education.common.pojo.LCatalogMenu;
import com.education.common.utils.ApiReturnObject;
import com.education.welco.pojo.LCatalogLabel;
import com.education.welco.pojo.LLabel;
import com.education.welco.pojo.LResources;
import com.education.welco.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 课程 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@RequestMapping("/lCatalog")
public class LCatalogController extends BaseController {


    @Autowired
    private ILCatalogService lCatalogService;
    @Autowired
    private ILCatalogMenuService catalogMenuService;
    @Autowired
    private ILLabelService labelService;
    @Autowired
    private ILCatalogLabelService catalogLabelService;
    @Autowired
    private ILResourcesService resourcesService;


    /**
     * feign : 查询全部平台课程
     */
    @GetMapping("/SelectCatalogList")
    public PageInfo selectCatalogList(Integer pageNum, Integer pageSize){
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        List<LCatalog> catalogList = lCatalogService.selectLCatalogList(null);
        PageInfo<LCatalog> pageInfo = new PageInfo<>(catalogList);
        return pageInfo;
    }

    /**
     * feign : 根据课程id查询该课程目录
     */
    @GetMapping("/SelectCatalogMenuList")
    public List<LCatalogMenu> SelectCatalogMenuList(Integer menuId) {
        List<LCatalogMenu> catalogMenuList = catalogMenuService.selectLCatalogMenuByMenuId1(menuId);
        //查询二级
        for (LCatalogMenu lCatalogMenu : catalogMenuList) {
            List<LCatalogMenu> catalogMenus = catalogMenuService.findByParentId(lCatalogMenu.getId());
            lCatalogMenu.setTwoMenu(catalogMenus);
            //查询三级
            for (LCatalogMenu catalogMenu : catalogMenus) {
                LCatalog lCatalog = new LCatalog();
                lCatalog.setParentId(catalogMenu.getId());
                List<LCatalog> lCatalogs = lCatalogService.selectLCatalogList(lCatalog);
                catalogMenu.setThreeMenu(lCatalogs);
            }
        }
        return catalogMenuList;
    }

    /**
     *  查询教学云课堂详情
     */
    @GetMapping("/selectOne")
    public ApiReturnObject selectOne(Integer id){
        if(id != null){
            //课程本身
            LCatalog catalog = lCatalogService.selectLCatalogById(id);
            if(catalog != null){
                catalog.setLabelList(Lists.newArrayList());
                //展示标签列表 根据id查出添加的标签
                LCatalogLabel catalogLabel = new LCatalogLabel();
                catalogLabel.setCatalogId(catalog.getId());
                //标签id集合
                List<Integer> list = catalogLabelService.selectLCatalogLabelListOnlyId(catalogLabel);
                if(list != null && list.size() > 0 ){
                    List<LLabel> lLabels = labelService.selectLLabelByIdList(list);
                    //添加标签集合
                    catalog.setLabelList(lLabels);
                }
                return new ApiReturnObject("200","查询成功",catalog);
            }
            return new ApiReturnObject("201","数据异常","");
        }
        return new ApiReturnObject("201","缺少参数","");
    }

    /**
     *  往标签中间表添加数据
     */
    public void addZhongJianBiao(List<String> list,LCatalogLabel lCatalogLabel){
        for (String s : list){
            //往标签表里面添加数据
            LLabel label = new LLabel();
            label.setLabelName(s);
            label.setCreateTime(new Date());
            labelService.insertLLabel(label);
            //往中间表里添加数据
            Integer id = label.getId();
            lCatalogLabel.setLabelId(id);
            catalogLabelService.insertLCatalogLabel(lCatalogLabel);
        }
    }

    /**
     *  添加平台课程
     */
    @PostMapping("/add")
    public ApiReturnObject add(@RequestBody LCatalog catalog){
        catalog.setCreateTime(new Date());
        catalog.setStudysum("0");
        catalog.setCatalogType("1");
        lCatalogService.insertLCatalog(catalog);
        Integer catalogId = catalog.getId();
        //往标签中间表中添加数据
        if(catalog.getTagList() != null && catalog.getTagList().size() > 0){
            LCatalogLabel lCatalogLabel = new LCatalogLabel();
            lCatalogLabel.setCatalogId(catalogId);
            addZhongJianBiao(catalog.getTagList(),lCatalogLabel);
        }
        return new ApiReturnObject("200","添加成功","");
    }

    /**
     *  修改平台课程
     */
    @PostMapping("/update")
    public ApiReturnObject update(@RequestBody LCatalog catalog){
        if(catalog.getId() != null){
            //修改标签列表 清空中间表
            catalogLabelService.deleteLCatalogLabelByCatalogId(catalog.getId());
            catalog.setStudysum("11");
            lCatalogService.updateLCatalog(catalog);
            //往标签中间表中添加数据
            if(catalog.getTagList() != null && catalog.getTagList().size() > 0){
                LCatalogLabel lCatalogLabel = new LCatalogLabel();
                lCatalogLabel.setCatalogId(catalog.getId());
                addZhongJianBiao(catalog.getTagList(),lCatalogLabel);
            }
            return new ApiReturnObject("200","修改成功","");
        }
        return new ApiReturnObject("201","缺少id参数","");
    }


    @PostMapping("del")
        public ApiReturnObject del(@RequestBody LCatalog catalog){
           if(catalog.getId() != null){
               //判断下面是否存在资源
               LResources resources = new LResources();
               resources.setRCatalogid(catalog.getId());
               List<LResources> sResources = resourcesService.selectLResourcesList(resources);
               if(sResources != null && sResources.size() > 0){
                   return new ApiReturnObject("202","该目录下面存在资源，请先删除资源","");
               }
               lCatalogService.deleteLCatalogById(catalog.getId());
               return new ApiReturnObject("200","删除成功","");
           }
           return new ApiReturnObject("201","缺少参数","");
       }


    /**
     * feign : 查询该服务是否启动
     */
    @GetMapping("/lCatalog")
    public String isWelco(){
        return "1";
    }
}